package com.kinon.gfcanteen_v1.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kinon.gfcanteen_v1.R;
import com.kinon.gfcanteen_v1.adapter.RecordAdapter;
import com.kinon.gfcanteen_v1.bean.BaseBean;
import com.kinon.gfcanteen_v1.customview.MyListView;
import com.kinon.gfcanteen_v1.network.CommonSubscriber;
import com.kinon.gfcanteen_v1.network.HttpManage;
import com.kinon.gfcanteen_v1.network.StrCallback;
import com.kinon.gfcanteen_v1.util.RxUtil;
import com.kinon.gfcanteen_v1.util.SPManager;
import com.kinon.gfcanteen_v1.bean.BalanceBean;
import com.kinon.gfcanteen_v1.view.base.BaseFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import io.reactivex.disposables.Disposable;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by lhqq on 2018-01-08.
 */

public class BalanceFragment extends BaseFragment implements View.OnClickListener{

    private View mView;
    private TextView tv_title;
    private TextView tv_cardId;
    private TextView tv_balance;
    private Button btn_scan;
    private MyListView list_balance;

    private int page=10;
    private int offset=0;
    private ArrayList<BalanceBean.Records> recordList;
    private RecordAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView=inflater.inflate(R.layout.fragment_balance_layout,container,false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        tv_title=mView.findViewById(R.id.tv_title);
        tv_cardId=mView.findViewById(R.id.tv_cardId);
        tv_balance=mView.findViewById(R.id.tv_balance);
        btn_scan=mView.findViewById(R.id.btn_scan);
        list_balance=mView.findViewById(R.id.list_balance);

        tv_title.setText("余额");
        recordList=new ArrayList<>();
         adapter=new RecordAdapter(recordList,getActivity());
        list_balance.setAdapter(adapter);
        addListener();
    }
    private void addListener(){
        btn_scan.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent=new Intent("com.summi.scan");
        intent.setPackage("com.sunmi.sunmiqrcodescanner");
        startActivityForResult(intent,1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e(TAG, "requestCode "+requestCode );
        Log.e(TAG, "resultCode "+resultCode );
        tv_cardId.setText("");
        if(requestCode==1 && data!=null){
            Bundle bundle=data.getExtras();
            ArrayList<HashMap<String, String>> result =
                    (ArrayList<HashMap<String, String>>) bundle.getSerializable("data");
            Iterator<HashMap<String, String>> it = result.iterator();
            String cardNo="";
            while (it.hasNext()){
                HashMap<String, String> hashMap = it.next();
                cardNo=hashMap.get("VALUE");
            }
            tv_cardId.setText(cardNo);

            Log.e(TAG, "cardNo== "+cardNo);
            getBalance(cardNo);
        }

    }
    private void getBalance(String card){
        Call<String> call=HttpManage.getInstance()
                .getUserByCode(SPManager.getInstance().getToken(),card,page,offset);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                Gson gson=new Gson();
                BalanceBean bean=gson.fromJson(data,BalanceBean.class);
                showView(bean);
            }

            @Override
            public void onError(int code, String error) {
               mToast(error);
            }
        });
    }

    private void showView(BalanceBean bean){
      tv_balance.setText(bean.getUser().getBalance());
        recordList.addAll(bean.getRecords());
        adapter.notifyDataSetChanged();
    }
}
